@extends('layout.default')

@section('layout.content')

<div class="space-18"></div>

<form class="form-horizontal" autocomplete="off" id="bookupdate" name="bookupdate">
  <div class="row">
  <div class="col-xs-12 col-md-6 col-md-offset-3">
    <div class="form-group">
      <div class="input-group">
        <input type="text" class="form-control input-lgr" id="isbn" name="isbn" value="{{$book->isbn}}" maxlength="13"
               placeholder="扫码或输入ISBN编码"
               autocomplete="off"/>
        <span class="input-group-btn">
        <button class="btn btn-lg btn-danger" type="submit" id="book_query">
          <i class="ace-icon fa fa-book"></i> 查询
        </button>
        <button class="btn btn-lg btn-primary" type="submit" id="book_submit">
          <i class="ace-icon fa fa-check"></i> 确定
        </button>
      </span>
      </div>
    </div>
  </div>  
</div>  
<div class="row">
  <div class="col-xs-12">

    <div class="tabs">

      <ul class="nav nav-tabs" id="myTab">
        <li class="active">
          <a data-toggle="tab" href="#home" aria-expanded="true">
            书籍信息
          </a>
        </li>

        <li class="">
          <a data-toggle="tab" href="#messages" aria-expanded="false">
            附加信息
          </a>
        </li>
      </ul>

      <div class="tab-content">
        <div id="home" class="tab-pane fade active in">
          <div class="row">
            <div class="col-md-2 col-xs-12 center">
              <!-- book image-->
              <span class="profile-picture">
                <img id="cover" name="cover" src="/assets/images/books/{{ $book->dbid }}.jpg" class="img-responsive">
                <input type="hidden" name="image" id="image" value="{{ $book->image }}"/>
              </span>
              <!-- book image end -->
            </div>
          
            <div class="col-sm-10 col-xs-12">
              <div class="form-group">
                <label class="col-sm-2 control-label no-padding-right" for="branch"> <b>所属分支：</b> </label>
                <label class="col-sm-8 control-label align-left">
                  <input type="hidden" name="branch_id" value="{{Auth('admin')->user()->branch->id}}">
                  {{Auth('admin')->user()->branch->fullname}} （{{Auth('admin')->user()->branch->name}}）
                </label>
              </div>

              <div class="form-group">
                <label class="col-sm-2 control-label no-padding-right" for="bookcategory"><i class="ace-icon fa fa-tag red"></i> <b>分类：</b></label>
                <div class="col-sm-8">
                  <div class="radio-inline no-padding-left" style="margin-top:-4px;">
                    @foreach($categories as $key => $category)
                      <label>
                        <input name="bookcategory" value="{{$key}}" type="radio"
                           class="ace" {{$book->bookcategory == $key?'checked':''}}/>
                        <span class="lbl store-block {{$category['class']}}"><b>{{$category['name']}}</b></span>
                      </label>
                    @endforeach
                  </div>
                </div>
              </div>


              <div class="form-group">
                <label class="col-sm-2 control-label no-padding-right" for="bookcase"><i class="ace-icon fa fa-tag red"></i> <b>书柜号：</b></label>
                <div class="col-sm-8">
                  <div data-toggle="buttons" class="btn-group">
                    @for ($i = 1; $i <= 10; $i++)
                    <label class="btn btn-orange {{$book->bookcase == $i?'active':''}}">
                      <input type="radio" name="bookcase" value="{{$i}}" {{$book->bookcase == $i?'checked':''}} />
                      <b>{{$i}}</b>
                    </label>
                    @endfor
                  </div>                 
                </div>
              </div>

              <div class="form-group">
                <label class="col-sm-2 control-label no-padding-right" for="bookshelf"><i class="ace-icon fa fa-tag red"></i> <b>书架号：</b></label>
                <div class="col-sm-8">
                  <div data-toggle="buttons" class="btn-group">
                    @for ($i = 1; $i <= 6; $i++)
                    <label class="btn btn-orange {{$book->bookshelf == $i?'active':''}}">
                      <input type="radio" name="bookshelf" value="{{$i}}" {{$book->bookshelf == $i?'checked':''}}/>
                      <b>{{$i}}</b>
                    </label>
                    @endfor
                  </div>                 
                </div>
              </div>

              <div class="form-group">
                <label class="col-sm-2 control-label no-padding-right" for="title"><i class="ace-icon fa fa-tag red"></i> <b>书名：</b></label>
                <div class="col-sm-7">
                  <input type="text" class="form-control" id="title" name="title" placeholder="书名" value="{{ $book->title }}"/>
                </div>
              </div>

              <div class="form-group">
                <label class="col-sm-2 control-label no-padding-right" for="donation_account"><i class="ace-icon fa fa-info-circle blue"></i> <b>捐赠用户：</b></label>
                <div class="col-sm-3">
                  <input type="text" class="form-control" name="donation_account" placeholder="捐赠用户账号" value="{{ $book->donation_account }}" />
                </div>
                <div class="col-sm-6 control-label align-left">
                  <span class="help-block smaller-80">如果填入捐赠用户，则该书将标记为赠书！</span>
                </div>
              </div>

              <div class="form-group">
                <label class="col-sm-2 control-label no-padding-right" for="author">作者：</label>
                <div class="col-sm-4">
                  <input type="text" class="form-control" id="author" name="author" placeholder="作者" value="{{ $book->author }}"/>
                </div>
                <label class="col-sm-1 control-label no-padding-right" for="num">册数</label>
                <div class="col-sm-2">
                  <input type="text" class="form-control" id="num" name="num" value="{{ $book->num }}" placeholder="册数"/>
                </div>
              </div>


              <div class="form-group">
                <label class="col-sm-2 control-label no-padding-right" for="publisher">出版社：</label>
                <div class="col-sm-4">
                  <input type="text" class="form-control" id="publisher" name="publisher" placeholder="出版社" value="{{ $book->publisher }}"/>
                </div>
              </div>

              <div class="form-group">
                <label class="col-sm-2 control-label no-padding-right" for="pubdate">出版日期：</label>
                <div class="col-sm-4">
                  <input type="text" class="form-control" id="pubdate" name="pubdate" placeholder="出版日期" value="{{ $book->pubdate }}"/>
                </div>
              </div>


              <div class="form-group">
                <label class="col-sm-2 control-label no-padding-right" for="price">价格：</label>
                <div class="col-sm-2">
                  <input type="text" class="form-control" id="price" name="price" placeholder="价格" value="{{ $book->price }}"/>
                </div>

                <label class="col-sm-1 control-label no-padding-right" for="pages">页数：</label>
                <div class="col-sm-2">
                  <input type="text" class="form-control" id="pages" name="pages" placeholder="页数" value="{{ $book->pages }}"/>
                </div>

                <label class="col-sm-1 control-label no-padding-right" for="binding">包装：</label>
                <div class="col-sm-2">
                  <input type="text" class="form-control" id="binding" name="binding" placeholder="包装" value="{{ $book->binding }}"/>
                </div>
              </div>

              <div class="form-group">
                <label class="col-sm-2 control-label no-padding-right" ></label>
                <div class="col-sm-6">
                  <div class="radio">
                    <label class="inline no-padding-left">
                      <input name="order" type="checkbox" value="9"
                             {{$book->order==9?'checked':''}} class="ace ace-checkbox-2 input-lg">
                      <span class="lbl"> 后序</span>
                    </label>
                  </div>
                </div>
              </div>

            </div>
          </div> <!-- .row -->

        </div>

        <div id="messages" class="tab-pane fade">
          <div class="row">
            <div class="col-xs-12">

              <div class="form-group">
                <label class="col-sm-1 control-label no-padding-right" for="dbid">DBID：</label>
                <div class="col-sm-4">
                  <input type="text" class="form-control" id="dbid" name="dbid" value="{{ $book->dbid }}"/>
                </div>
              </div>


              <div class="form-group">
                <label class="col-sm-1 control-label no-padding-right" for="origin_title">原书名：</label>
                <div class="col-sm-5">
                  <input type="text" class="form-control" id="origin_title" name="origin_title" placeholder="原书名" value="{{ $book->origin_title }}"/>
                </div>
              
                <label class="col-sm-1 control-label no-padding-right" for="subtitle">子标题：</label>
                <div class="col-sm-5">
                  <input type="text" class="form-control" id="subtitle" name="subtitle" placeholder="子标题" value="{{ $book->subtitle }}"/>
                </div>
              </div>


              <div class="form-group">
                <label class="col-sm-1 control-label no-padding-right" for="translator">翻译：</label>
                <div class="col-sm-4">
                  <input type="text" class="form-control" id="translator" name="translator" placeholder="翻译" value="{{ $book->translator }}"/>
                </div>
              
                <label class="col-sm-2 control-label no-padding-right" for="series">丛书：</label>
                <div class="col-sm-4">
                  <input type="text" class="form-control" id="series" name="series" value="{{ $book->series }}"/>
                </div>
              </div>


              <div class="form-group">
                <label class="col-sm-1 control-label no-padding-right" for="summary">摘要：</label>
                <div class="col-sm-5">
                  <textarea class="form-control" rows="8" id="summary" name="summary"
                            placeholder="摘要内容">{{ $book->summary }}</textarea>
                </div>
             
                <label class="col-sm-1 control-label no-padding-right" for="catalog">目录：</label>
                <div class="col-sm-5">
                  <textarea class="form-control" rows="8" id="catalog" name="catalog">{{ $book->catalog }}</textarea>
                </div>
              </div>


              <div class="form-group">
                <label class="col-sm-1 control-label no-padding-right" for="isbn10">ISBN(10)：</label>
                <div class="col-sm-4">
                  <input type="text" class="form-control" id="isbn10" name="isbn10" value="{{ $book->isbn10 }}"/>
                </div>
              
                <label class="col-sm-2 control-label no-padding-right" for="isbn13">ISBN(13)：</label>
                <div class="col-sm-4">
                  <input type="text" class="form-control" id="isbn13" name="isbn13" value="{{ $book->isbn13 }}"/>
                </div>
              </div>

              <div class="form-group">
                <label class="col-sm-1 control-label no-padding-right" for="author_intro">作者简介：</label>
                <div class="col-sm-5">
                  <textarea class="form-control" rows="6" id="author_intro" name="author_intro"
                            placeholder="作者简介">{{ $book->author_intro }}</textarea>
                </div>
              
                <label class="col-sm-1 control-label no-padding-right" for="url">URL：</label>
                <div class="col-sm-5">
                  <input type="text" class="form-control" id="url" name="url" value="{{ $book->url }}"/>
                </div>
              </div>
            </div>
          </div>
        </div>
       
      </div>
    </div>
    
  </div>
</div>
</form>

@stop


@section('javascript')
  <script src="//cdn.bootcss.com/jquery-validate/1.19.1/jquery.validate.min.js"></script>
  <script src="//cdn.bootcss.com/jqueryui/1.12.1/jquery-ui.min.js"></script>
  <script src="{{URL::asset('assets/js/jquery-validate-methods.js')}}"></script>
  <script type="text/javascript">

      $(function () {

        var account = null;

          $("#isbn").focus();

          $('#book_query').on('click', function () {
              $(this).initform();
              var isbn = $('#isbn').val();
              //isbn = isbn.replace(/-/gm,'');
              //9787111128069 //9787506021715 //9787534275166 //9787504217851
              var request = 'https://api.douban.com/v2/book/isbn/' + isbn+'?apikey=0df993c66c0c636e29ecbb5344252a4a';
              //alert(isbn);
              $.ajax({
                  url: request,
                  type: 'get',
                  dataType: 'jsonp',
                  success: function (response) {
                      //console.log(response) ;
                      $(this).fillback(response);
                  }
              });
              return false;
          });

          $("input[name='donation_account']").autocomplete({
            source : function(request, response) {
              $.get('/users', {
                  'term':request.term,
                  'type':2,
                }, function(data) {
                  response($.map(data,function(item) {
                    console.log(item);
                    return { 
                      label: item.account+' - '+item.name, 
                      account:item.account
                    }
                  }));
                  
                },'json');
              },

            select:function(e, ui) {
              console.log(ui.item.account);
              account = ui.item.account;

            },
            minLength: 3,
            maxlength: 5,
          });

          /**
           表单前端验证
           */
          $("#bookupdate").validate({
              rules: {
                  isbn: {required: true},
                  title: {required: true},
                  bookcategory: {required: true},
                  bookcase: {required: true},
                  bookshelf: {required: true},
                  account:{digits: true,minlength: 5,maxlength: 5}
              },
              messages: {
                  isbn: {required: "请填写ISBN"},
                  title: {required: "请填写书名"},
                  bookcategory: {required: "请选择书籍分类"},
                  bookcase: {required: "请选择书柜号"},
                  bookshelf: {required: "请选择书架号"},
                  account: {                      
                      digits: "用户账号格式不正确",
                      maxlength: "用户账号格式不正确",
                      minlength: "用户账号格式不正确"
                  }
              },

              onfocusout: false,
              onkeyup: false,

              showErrors: function (errorMap, errorList) {
                  $.each(errorList, function (i, v) {
                      layer.tips(v.message, v.element, {tips: 3, time: 2000});
                      return false;
                  });
              },

              submitHandler: function (form) {
                  
                  uri =
                      $.post('{{route('book.update',$book->id)}}', {
                          '_token': $("input[name='_token']").attr('value'),
                          '_method': 'put',
                          'dbid': $("input[name='dbid']").val(),
                          'isbn': $("input[name='isbn']").val(),
                          'title': $("input[name='title']").val(),
                          'bookcase': $("input[name='bookcase']:checked").val(),
                          'bookshelf': $("input[name='bookshelf']:checked").val(),
                          'bookcategory': $("input[name='bookcategory']:checked").val(),
                          'branch_id': $("input[name='branch_id']").val(),
                          'donation_account': account,
                          'isbn10': $("input[name='isbn10']").val(),
                          'isbn13': $("input[name='isbn13']").val(),
                          'author': $("input[name='author']").val(),
                          'origin_title': $("input[name='origin_title']").val(),
                          'subtitle': $("input[name='subtitle']").val(),
                          'url': $("input[name='url']").val(),
                          'image': $("input[name='image']").val(),
                          'translator': $("input[name='translator']").val(),
                          'publisher': $("input[name='publisher']").val(),
                          'pubdate': $("input[name='pubdate']").val(),
                          'binding': $("input[name='binding']").val(),
                          'price': $("input[name='price']").val(),
                          'pages': $("input[name='pages']").val(),
                          'series': $("input[name='series']").val(),
                          'author_intro': $("textarea[name='author_intro']").val(),
                          'summary': $("textarea[name='summary']").val(),
                          'catalog': $("textarea[name='catalog']").val(),
                          'order': $("input[name='order']:checked").val(),
                          'is_submit': true
                      }, function (data) {
                          layer.msg(data.message, {time:1000, shift: -1}, function () {
                              if (data.status === true && data.url != null) {
                                  $(window).attr('location', data.url);
                              }
                          });

                      }, 'json').error(function (data) {
                          layer.msg(data.responseJSON.message);
                      });
              }


          });
      })

      $.fn.fillback = function (data) {

          $.each(data, function (key, value) {

              if (typeof (value) != "object") {
                  switch (key) {
                      case 'id':
                          $("#dbid").val(data['id']);
                          break;
                      case 'image':
                          $('#cover').attr("src", data['image']);
                          $('#' + key).val(data['image']);
                          break;
                      default:
                          $("#" + key).val(data[key]);
                          break;
                  }
              } else {
                  var str = '';
                  switch (key) {
                      case 'series':
                          str = value['title'];
                          break;
                      /*case 'tags':
                        for(var i = 0; i < value.length; i++){
                          str += value[i]['name'] + '  ';
                        }
                        break; */
                      default:
                          for (var i = 0; i < value.length; i++) {
                              str += value[i] + ' ';
                          }
                          break;
                  }

                  $("#" + key).val(str);
              }
          });

      }

      $.fn.initform = function () {
          $("input[type=text]:not(input[name='isbn'],input[name='num']),textarea").val('');
          $("#image").attr("src", '');
      }

  </script>

@stop